package ro.xblue.translator;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: Module.java,v 1.1 2004/10/24 23:37:03 mihaipostelnicu Exp $
 */
public class Module
{
      Hashtable messages;
      File wdir;
      String name;
      /**
       * limbile pt. modulul this
       */
      String[] avail_langs;

      public String[] getAvailableLangs()
      {
            return avail_langs;
      }

      public Module( File wdir )
      {
            if ( ( wdir == null ) || ( !wdir.exists() ) || ( !wdir.isDirectory() ) )
                  throw new RuntimeException( "Incearca cu un dir valid" );

            this.wdir = wdir;
            this.messages = new Hashtable();
            this.name = wdir.getName();
            reload();

            make_langs();
      }

      private void make_langs()
      {
            this.avail_langs = new String[ messages.size() ];

            Enumeration en = messages.keys();
            int i = 0;
            while ( en.hasMoreElements() )
            {
                  avail_langs[ i++ ] = ( String ) en.nextElement();
            }
      }

      public void reload()
      {
            File[] files = wdir.listFiles();
            if ( ( files == null ) || ( files.length < 1 ) )
                  return;

            messages.clear();

            for ( int i = 0; i < files.length; i++ )
            {
                  String file_name = files[ i ].getName();
                  if ( !file_name.endsWith( ".properties" ) )
                        continue;

                  String language_code = file_name.
                        substring( 0, file_name.lastIndexOf( '.' ) ).toLowerCase();
                  Properties props = new Properties();
                  try
                  {
                        FileInputStream fis = new FileInputStream( files[ i ] );
                        props.load( fis );
                        fis.close();
                  }
                  catch ( IOException e )
                  {
                        e.printStackTrace();
                  }

                  if ( props.size() > 0 )
                  {
                        messages.put( language_code, props );
                  }
            }

            make_langs();
      }

      public String getMessage( String lang, String label )
      {
            Properties msgs = ( Properties ) messages.get( lang );
            if ( msgs == null ) return null;

            return msgs.getProperty( label );
      }

      public Properties getMessages( String lang )
      {
            if ( lang == null ) return null;
            return ( Properties ) messages.get( lang );
      }

      public String getName()
      {
            return name;
      }
}
